<?php declare(strict_types = 1);


namespace App\Service;


use Swoole\Exception;

class HashPassWordService
{
    /**
     *  加密
     * @param $pwd
     * @param $salt
     * @return false|string|null
     */
    public function genPassWord($pwd, $salt)
    {
        $tmpPwd = $pwd . $salt;
        return password_hash($tmpPwd, PASSWORD_DEFAULT);
    }

    /**
     * 验证
     * @param $pwd
     * @param $salt
     * @param $hash
     * @return bool
     */
    public function verify($pwd,$salt,$hash): bool
    {
        $tmpPwd = $pwd . $salt;
        return password_verify($tmpPwd,$hash);
    }

    /**
     * 验证密码，如果不对抛出错误
     * @param $pwd
     * @param $salt
     * @param $hash
     * @throws Exception
     */
    public function verifyCast($pwd, $salt, $hash): void
    {
        if(!$this->verify($pwd,$salt,$hash)){
            throw new Exception('账号或密码错误');
        }
    }
    

}